﻿using System;
using Csla.Xaml;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;
using Accounting.Library;


namespace csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.TSCD_MuaTaiSan
{
    public class TSCD_MuaTaiSan_ViewModel : ViewModel<Accounting.Library.TSCD_MuaTaiSan>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public delegate void DKNV_EventHandler(object sender, DieuKienNghiepVuEventArgs e);
        public delegate void TSCDIdChanged_EventHandler(object sender, TSCDIdChangedEventArgs e);
        public delegate void TKCoIdChanged_EventHandler(object sender, TKCoIdChangedEventArgs e);
        public delegate void ThueSuatIdChanged_EventHandler(object sender, ThueSuatIdChangedEventArgs e);
        
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

        public event DKNV_EventHandler stateChanged; 
 
        public event EventHandler TCHangHoaChanged;
        public event EventHandler LoaiTienTeChanged;
       
        
        public event TSCDIdChanged_EventHandler TSCDIdChanged;
        public event TKCoIdChanged_EventHandler TKCoIdChanged;
        public event ThueSuatIdChanged_EventHandler ThueSuatIdChanged;  
        #endregion

        #region Refresh Model
        public TSCD_MuaTaiSan_ViewModel()
        {
            BeginRefresh("NewTSCD_MuaTaiSan");
            
        }

        public TSCD_MuaTaiSan_ViewModel(int Khoa)
        {
            BeginRefresh("GetTSCD_MuaTaiSan", Khoa);
           
        }

        public void NewTSCD_MuaTaiSanViewModel()
        {
            BeginRefresh("NewTSCD_MuaTaiSan");
            
        }

        public void GetTSCD_MuaTaiSanViewModel(int Khoa)
        {
            BeginRefresh("GetTSCD_MuaTaiSan", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewTSCD_MuaTaiSanViewModel();
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.TSCD_MuaTaiSan_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ChiTietChild>>(TSCD_MuaTaiSan_ChiTiets_AddedNew);
                Model.TSCD_MuaTaiSan_Thues.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ThueChild>>(TSCD_MuaTaiSan_Thues_AddedNew);
                Model.TSCD_MuaTaiSan_ChiPhiMuas.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ChiPhiMuaChild>>(TSCD_MuaTaiSan_ChiPhiMuas_AddedNew);
                
                Model.TSCD_MuaTaiSan_ThueNKs.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ThueNKChild>>(TSCD_MuaTaiSan_ThueNKs_AddedNew);
                Model.TSCD_MuaTaiSan_ThueTTDBs.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ThueTTDBChild>>(TSCD_MuaTaiSan_ThueTTDBs_AddedNew);
                Model.NhomDKNVChanged += new EventHandler < DieuKienNghiepVuEventArgs>(OnStateChanged);
                Model.TinhChatHangHoaChanged += new EventHandler(OnTinhChatHangHoaChanged);
                Model.TSCDIdChanged += new EventHandler<TSCDIdChangedEventArgs>(OnTSCDIdChanged);
                Model.TKCoChanged += new EventHandler<TKCoIdChangedEventArgs>(OnTKCoChanged);
                Model.LoaiTienTeChanged+= new EventHandler(OnLoaiTienTeChanged);
               
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);
                Model.ThueSuatIdChanged += new EventHandler<ThueSuatIdChangedEventArgs>(OnThueSuatIdChanged);
               
            }
        }
        #endregion
        
        #region Add TSCD_MuaTaiSan Details Item
        public void AddTSCD_MuaTaiSan_ChiTietItem()
        {
            Model.TSCD_MuaTaiSan_ChiTiets.AddNewItem(Model.Khoa);
        }

        public void AddTSCD_MuaTaiSan_ThueItem()
        {
            Model.TSCD_MuaTaiSan_Thues.AddNewItem(Model.Khoa);
        }

        public void AddTSCD_MuaTaiSan_ChiPhiMuaItem()
        {
            Model.TSCD_MuaTaiSan_ChiPhiMuas.AddNewItem(Model.Khoa);
        }

       
        public void AddTSCD_MuaTaiSan_ThueNKItem()
        {
            Model.TSCD_MuaTaiSan_ThueNKs.AddNewItem(Model.Khoa);
        }

        public void AddTSCD_MuaTaiSan_ThueTTDBItem()
        {
            Model.TSCD_MuaTaiSan_ThueTTDBs.AddNewItem(Model.Khoa);
        }



        void TSCD_MuaTaiSan_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TSCD_MuaTaiSan_ChiTiets"));
        }      

        void TSCD_MuaTaiSan_Thues_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ThueChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TSCD_MuaTaiSan_Thues"));
        }

        void TSCD_MuaTaiSan_ChiPhiMuas_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ChiPhiMuaChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TSCD_MuaTaiSan_ChiPhiMuas"));
        }

       

        void TSCD_MuaTaiSan_ThueNKs_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ThueNKChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TSCD_MuaTaiSan_ThueNKs"));
        }

        void TSCD_MuaTaiSan_ThueTTDBs_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TSCD_MuaTaiSan_ThueTTDBChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TSCD_MuaTaiSan_ThueTTDBs"));
        }


        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

        public void OnStateChanged(object sender, DieuKienNghiepVuEventArgs e)
        {
            if (stateChanged != null)
            {
                stateChanged(sender, e);
            }
          
        }
        public void OnTSCDIdChanged(object sender, TSCDIdChangedEventArgs e)
        {
            if (TSCDIdChanged != null)
            {
                TSCDIdChanged(sender, e);
            }

        }
        public void OnTinhChatHangHoaChanged(object sender, EventArgs e)
        {
            if (TCHangHoaChanged != null)
            {
                TCHangHoaChanged(sender, e);
            }

        }
        public void OnLoaiTienTeChanged(object sender, EventArgs e)
        {
            if (LoaiTienTeChanged != null)
            {
                LoaiTienTeChanged(sender, e);
            }
        }

       
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }
        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        public void OnTKCoChanged(object sender,TKCoIdChangedEventArgs  e)
        {
            if (TKCoIdChanged != null)
            {
                TKCoIdChanged(sender, e);
            }

        }

        public void OnThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            if (ThueSuatIdChanged != null)
            {
                ThueSuatIdChanged(sender, e);
            }

        }
        #endregion

        #region Delete TSCD_MuaTaiSan Details Item

        public void DeleteTSCD_MuaTaiSan_ChiTietItem(int Index)
        {
            Model.TSCD_MuaTaiSan_ChiTiets.DeleteItem(Index);
        }

        public void DeleteTSCD_MuaTaiSan_ThueItem(int Index)
        {
            Model.TSCD_MuaTaiSan_Thues.DeleteItem(Index);
        }
        public void DeleteTSCD_MuaTaiSan_ChiPhiMuaItem(int Index)
        {
            Model.TSCD_MuaTaiSan_ChiPhiMuas.DeleteItem(Index);
        }

        
        public void DeleteTSCD_MuaTaiSan_ThueNKItem(int Index)
        {
            Model.TSCD_MuaTaiSan_ThueNKs.DeleteItem(Index);
        }
        public void DeleteTSCD_MuaTaiSan_ThueTTDBItem(int Index)
        {
            Model.TSCD_MuaTaiSan_ThueTTDBs.DeleteItem(Index);
        }

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_TSCD_MuaTaiSan;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetTSCD_MuaTaiSan", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
